<template>
  <view class="share-page">
    <view class="action">
      <button
        v-show="!loading"
        class="action-button start-self"
        type="primary"
        @tap="getUserInfo"
      >
        欢迎使用文朵小程序
      </button>
    </view>
  </view>
</template>

<script lang="ts">
import { auth } from "../../services/auth-service";
// import { getCustomerInfo } from "../../services/user-service";
import {
  LSBluetoothPlugin,
  isBluetoothAvailable,
  addEventListener
} from "../../utils/deviceMananer.js";
import Taro from "@tarojs/taro";
export default {
  components: {},
  data() {
    return {
      isLogin: true,
      loading: true,
      isExit: false
    };
  },
  methods: {
    navigateToNextPage() {
      const url = this.isExit ? "/pages/index/index" : "/pages/user-info/index";
      Taro.redirectTo({
        url
      });
    },
    share() {
      Taro.showShareMenu({
        withShareTicket: true
      });
      console.log(1);
    },
    LSDeviceUserDataSync(customerId) {
      addEventListener("bluetoothStateChange", res => {
        console.log(`蓝牙状态发生改变${JSON.stringify(res)}`);
      });
      //注册设备连接监听
      addEventListener("connectionStateChange", device => {
        console.log(`设备连接状态发现改变${JSON.stringify(device)}`);
      });
      //注册数据回调监听
      addEventListener("dataChanged", (deviceMac, dataType, data, dataStr) => {
        console.log(dataStr);
      });

      // initManager(() => {
      //   "蓝牙设备初始化";
      // });

      const LSDeviceLoginConfig = {
        appId: "wx43b1ab446b5db1e0",
        tenantName: "wenduo-embed", // 租户名称
        tenantId: 23, // 租户id
        subscriptionId: 2301, // 订阅id
        associatedId: customerId, // 第三方关联userId
        debug: true, // 打开H5的vconsole
        env: "online" // 接口环境：beta测试，online生产,
      };

      const that = this;
      LSBluetoothPlugin.login({
        ...LSDeviceLoginConfig,
        success(res) {
          console.info(res);
          console.info("用户信息已同步!");

          //判断手机蓝牙是否可用
          if (isBluetoothAvailable()) {
            console.log("蓝牙已开启！");
            //连接设备
            that.connectDevice();
          } else {
            //提示打开手机蓝牙
            console.log("蓝牙未开启！");
          }
        },
        fail(error) {
          console.error(error);
        }
      });
    },
    getUserInfo() {
      const _that = this;
      Taro.showModal({
        title: "温馨提示",
        content: "点击获取微信头像和昵称",
        success: res => {
          if (res.confirm) {
            Taro.getUserProfile({
              desc: "用于显示用户微信头像和昵称",
              success: res => {
                this.$store.commit("setUserInfo", {
                  nickName: res.userInfo.nickName,
                  avatarUrl: res.userInfo.avatarUrl
                });
              },
              complete: () => {
                _that.navigateToNextPage();
              }
            });
          } else {
            _that.navigateToNextPage();
          }
        }
      });
    }
  },

  async created() {
    let loginResult = await Taro.login();
    let authResult = await auth(loginResult.code);
    Taro.setStorageSync("sessionKey", authResult.data.data.sessionKey);
    this.isExit = authResult.data.data.isExist;
    // if (this.isExit) {
    //   const customerUserInfo = await getCustomerInfo();
    //   const customerId = customerUserInfo.id;
    //   this.LSDeviceUserDataSync(customerId);
    // }
    this.loading = false;
  },
  mounted() {},

  // onLoad
  onLoad() {},

  // onReady
  onReady() {},

  // 对应 onShow
  onShow() {},

  // 对应 onHide
  onHide() {},

  // 对应 onPullDownRefresh
  onPullDownRefresh() {}
};
</script>
<style lang="less">
.share-page {
  height: 100vh;
  width: 100%;
  position: relative;
  background-image: url(https://manbingguanli.oss-cn-beijing.aliyuncs.com/wenduo/4dadc41a90634561805e3ad7f8f4397fhomebg.jpg);
  background-size: 100% auto;
  background-repeat: no-repeat;
  background-color: #dcd2fc;

  .action {
    position: absolute;
    width: 100%;
    display: flex;
    bottom: 0;
    justify-content: space-between;
    .btn-icon {
      display: inline-block;
      margin-bottom: 0.35rem;
      margin-right: 0.5rem;
    }
    .action-button {
      margin: 1rem 1rem 2rem 1rem;
      border-radius: 30px;
      border: none;
      background-color: #6c63ff;
      flex: 1;
    }

    .start-self {
      flex: 2;
    }
  }
}
</style>
